var paginationTotal = 1;//记录总数
var sortColumn = "";
var sortMethod = "";
$(function(){
	initData(0);
	$("#confirmDialog").dialog({
			autoOpen:false,
			modal:true,
			show:"slide"
	});
	
	$(".search").click(function(){
		initData(0);
	});
	
	$(".toEnter").keyup(function(e){
		var key = e.which;
		if(key==13){
			initData(0);
		}
	});
	
	$("#ckAll").click(function(){
		$("[name=checked]").attr("checked",$(this).attr("checked")=="checked"?true:false);
	});
	
	$("#SwichBrowseType").click(function(){
		if($(this).attr("status") == "0"){
			$(".subTabTr").show();
			$(this).attr("status","1");
		}else{
			$(".subTabTr").hide();
			$(this).attr("status","0");
		}
	});
	
	$(".sort").click(function(){
		sortColumn = $(this).attr("sortColumn");
		sortMethod = $(this).attr("sortMethod");
		$(".sort").children(".up").remove();
		if(sortMethod == 'asc'){
			$(this).attr("sortMethod","desc");
			$(this).append("<span class='up'>↑</span>");
		}else{
			$(this).attr("sortMethod","asc");
			$(this).append("<span class='up'>↓</span>");
		}
		
		initData(paginationCurrentPage-1);
	});
	
	$(".detail").live("click",function(){
		window.open("/product/receiving/detail/code/"+$(this).attr("receivingcode"));
	});
	
	$(".receive").live("click",function(){
		var obj = this;
		$.ajax({
			url:"/product/receiving/check-has-first/code/"+$(this).attr("receivingcode"),
			dataType:"json",
			success:function(json){
				if(json){
					$("#confirmDialog").dialog("option","buttons",[{
						text:"<{$lang.yes}>",
						click:function(){
							window.open("/product/receiving/receive/code/"+$(obj).attr("receivingcode"));
							$(this).dialog("close");
						}
					},{
						text:"<{$lang.no}>",
						click:function(){
							$(this).dialog("close");
						}
					}]).html("<{$lang.first}><{$lang.receive}>?").dialog("open");
					
				}else{
					window.open("/product/receiving/receive/code/"+$(obj).attr("receivingcode"));
				}
			}
		});
		
	});
	
	$('.toggleButton').click(function(){
		if($(this).attr("status") != 1){
			$(".detailInfo").hide();
			$(this).attr("status",1);
			$(this).val("<{$lang.folder}>");
		}else{
			$(".detailInfo").show();
			$(this).attr("status",0);
			$(this).val("<{$lang.unfolder}>");
		}
	});

});

function loadData(page, pageSize){
	var receivingCode = $("[name=receivingCode]").val();
	var status = $("[name=status]").val();
	var refId = $("[name=refId]").val();
	var customerId = $("[name=customerId]").val();
    $.ajax({
		url: "/product/receiving/loadlist/page/" + page + "/pageSize/" + pageSize,
        async: false,
        dataType: "json",
		type:"post",
		data:{"receivingCode":receivingCode,
			"status":status,"refId":refId,
			"customerId":customerId,
			"sortColumn":sortColumn,"sortMethod":sortMethod},
        success: function(json) {
            var html = "";
			paginationTotal = json.total;
			if(json.ask){
				var i = (page == 1 ? 1 : pageSize * (page - 1) + 1);
				$.each(json.result,function(k,v){
				  //$(".myTab tr:even").addClass("even-tr");
					html += '<tr><td>'+ (i++) +'</td>';			
					html += '<td class="showDetail toggleCode" receivingId="'+v.receiving_id+'">'+v.receiving_code+'</td>';
					html += '<td>'+v.customer_id+'</td>';
					html += '<td>'+v.statusText+'</td>';					
					html += '<td>'+v.ref_id+'</td>';
					
					html += '<td>'+v.orderTypeText+'</td>';
					html += '<td>'+v.created+'</td>';
					html += '<td>'+v.description+'</td>';					
					html += '<td>'+v.unNormal+'</td>';					
					
					html += '<td>';
					 if(v.status == 1){
						html += '<img src="/images/icon/printer.gif" title="<{$lang.print}><{$lang.items}>" style="cursor:pointer" receivingcode="'+v.receiving_code+'" class="detail"/>&nbsp;&nbsp;';
					 }
					// if(v.status == 1 || v.status == 2){
						html += '<img src="/images/icon/edit.gif" title="<{$lang.receive}>" style="cursor:pointer" receivingcode="'+v.receiving_code+'" class="receive"/>';
					// }
					html += '</td>';
					html += '</tr>';
					html += '';
					
					// 每次 的<{$lang.product}>具体<{$lang.msg}>
					html += '<tr id="detailInfo'+v.receiving_id+'" class="detailInfo" style="display:none;font-style:italic;background:#E2F1F9;">';
					html += '<td colspan="9">';
					html += '<table  width="100%"><tr style="font-weight:bold;">';
					html += '<td><{$lang.sku}>No</td>';
					html += '<td><{$lang.sku}>Name</td>';
					html += '<td>Weight</td>';
					html += '<td>OrderQty</td>';
					
					html += '<td>ReceivedQty</td>';
					html += '<td>Non-LabeledQty</td>';
					html += '<td>IsPackaged</td>';
					html += '<td>Remark</td>';
					html += '</tr>';
					
					$.each(v.products,function(pK,pV){
						html += '<tr>';
						html += '<td>'+pV.<{$lang.sku}>No+'</td>';
						html += '<td>'+pV.<{$lang.sku}>Name+'</td>';
						html += '<td>'+pV.weight+'</td>';
						html += '<td>'+pV.order_qty+'</td>';
						
						html += '<td>'+pV.received_qty+'</td>';
						html += '<td>'+pV.non_labeled_qty+'</td>';
						html += '<td>'+pV.packaged+'</td>';
						html += '<td>'+pV.remark+'</td>';
						html += '</tr>';
					});
					html += '</table></td></tr>';
				});				
				$("#loadData").html(html);
				$(".toggleCode").click(function(){
					$("#detailInfo"+$(this).attr("receivingId")).toggle();
				});

			}else{
				$("#loadData").html("<tr><td colspan='9' align='center'><{$lang.no_data}></td></tr>");
			}
        }
    });
}